<template>
  <view class="hospital3d-container">
    <!-- 楼层选择器 -->
    <view class="floor-selector">
      <view 
        v-for="floor in floors" 
        :key="floor.id"
        :class="['floor-item', currentFloor === floor.id ? 'active' : '']"
        @tap="switchFloor(floor.id)"
      >
        {{ floor.name }}
      </view>
    </view>

    <!-- 3D视图容器 -->
    <view class="map-container">
      <!-- 楼层平面图 -->
      <view class="floor-map" :style="{ transform: `rotateX(${rotation.x}deg) rotateY(${rotation.y}deg)` }">
        <!-- 科室标记点 -->
        <view 
          v-for="department in currentFloorData.departments" 
          :key="department.id"
          class="department-marker"
          :style="{ left: department.x + '%', top: department.y + '%' }"
          @tap="showDepartmentInfo(department)"
        >
          <view class="marker-dot"></view>
          <text class="marker-label">{{ department.name }}</text>
        </view>
      </view>
    </view>

    <!-- 科室信息弹窗 -->
    <view class="department-popup" v-if="selectedDepartment">
      <view class="popup-header">
        <text class="popup-title">{{ selectedDepartment.name }}</text>
        <text class="popup-close" @tap="selectedDepartment = null">×</text>
      </view>
      <view class="popup-content">
        <view class="info-item">
          <text class="label">位置：</text>
          <text>{{ selectedDepartment.location }}</text>
        </view>
        <view class="info-item">
          <text class="label">科室主任：</text>
          <text>{{ selectedDepartment.director }}</text>
        </view>
        <view class="info-item">
          <text class="label">接诊时间：</text>
          <text>{{ selectedDepartment.hours }}</text>
        </view>
        <view class="department-description">
          {{ selectedDepartment.description }}
        </view>
      </view>
    </view>

    <!-- 操作提示 -->
    <view class="controls-hint">
      <text>拖动查看不同角度 | 点击查看科室详情</text>
    </view>
  </view>
</template>

<script>
export default {
  data() {
    return {
      floors: [
        { id: 1, name: '1F' },
        { id: 2, name: '2F' },
        { id: 3, name: '3F' },
        { id: 4, name: '4F' },
      ],
      currentFloor: 1,
      rotation: {
        x: 45,
        y: 45
      },
      isDragging: false,
      lastTouch: null,
      selectedDepartment: null,
      floorData: {
        1: {
          departments: [
            {
              id: 1,
              name: '急诊科',
              x: 20,
              y: 30,
              location: '一楼东侧',
              director: '张医生',
              hours: '24小时',
              description: '提供24小时急诊服务，配备专业的急救设备和人员。'
            },
            {
              id: 2,
              name: '挂号处',
              x: 50,
              y: 20,
              location: '一楼大厅',
              director: '李主任',
              hours: '07:00-21:00',
              description: '提供现场挂号、预约挂号、医保卡办理等服务。'
            },
            {
              id: 3,
              name: '药房',
              x: 80,
              y: 40,
              location: '一楼西侧',
              director: '王主任',
              hours: '08:00-22:00',
              description: '配备专业药剂师，提供处方药和非处方药的发放服务。'
            }
          ]
        },
        2: {
          departments: [
            {
              id: 4,
              name: '内科',
              x: 30,
              y: 40,
              location: '二楼东侧',
              director: '刘主任',
              hours: '08:00-17:30',
              description: '包括呼吸内科、消化内科、心内科等专业科室。'
            }
          ]
        }
      }
    }
  },

  computed: {
    currentFloorData() {
      return this.floorData[this.currentFloor] || { departments: [] }
    }
  },

  onLoad() {
    // 初始化触摸事件监听
    this.initTouchEvents()
  },

  methods: {
    switchFloor(floorId) {
      this.currentFloor = floorId
      this.selectedDepartment = null
    },

    initTouchEvents() {
      // 触摸开始
      this.touchStartHandler = (e) => {
        this.isDragging = true
        this.lastTouch = {
          x: e.touches[0].clientX,
          y: e.touches[0].clientY
        }
      }

      // 触摸移动
      this.touchMoveHandler = (e) => {
        if (!this.isDragging || !this.lastTouch) return

        const deltaX = e.touches[0].clientX - this.lastTouch.x
        const deltaY = e.touches[0].clientY - this.lastTouch.y

        this.rotation.y += deltaX * 0.5
        this.rotation.x = Math.max(-60, Math.min(60, this.rotation.x + deltaY * 0.5))

        this.lastTouch = {
          x: e.touches[0].clientX,
          y: e.touches[0].clientY
        }
      }

      // 触摸结束
      this.touchEndHandler = () => {
        this.isDragging = false
        this.lastTouch = null
      }
    },

    showDepartmentInfo(department) {
      this.selectedDepartment = department
    }
  }
}
</script>

<style lang="scss">
.hospital3d-container {
  height: 100vh;
  background: #f5f5f5;
  position: relative;
  overflow: hidden;

  .floor-selector {
    position: fixed;
    right: 20rpx;
    top: 50%;
    transform: translateY(-50%);
    z-index: 100;
    display: flex;
    flex-direction: column;
    gap: 10rpx;

    .floor-item {
      width: 80rpx;
      height: 80rpx;
      background: rgba(255, 255, 255, 0.9);
      border-radius: 50%;
      display: flex;
      align-items: center;
      justify-content: center;
      font-size: 28rpx;
      color: #666;
      box-shadow: 0 2rpx 10rpx rgba(0, 0, 0, 0.1);
      transition: all 0.3s ease;

      &.active {
        background: #007AFF;
        color: #fff;
      }
    }
  }

  .map-container {
    width: 100%;
    height: 70vh;
    perspective: 1000px;
    display: flex;
    align-items: center;
    justify-content: center;

    .floor-map {
      width: 80%;
      height: 80%;
      background: rgba(255, 255, 255, 0.9);
      border-radius: 20rpx;
      position: relative;
      transform-style: preserve-3d;
      transition: transform 0.1s ease;
      box-shadow: 0 10rpx 30rpx rgba(0, 0, 0, 0.2);

      &::after {
        content: '';
        position: absolute;
        bottom: -20rpx;
        left: 5%;
        right: 5%;
        height: 20rpx;
        background: rgba(0, 0, 0, 0.1);
        filter: blur(10rpx);
        border-radius: 50%;
      }
    }
  }

  .department-marker {
    position: absolute;
    transform: translate(-50%, -50%);
    cursor: pointer;

    .marker-dot {
      width: 20rpx;
      height: 20rpx;
      background: #007AFF;
      border-radius: 50%;
      position: relative;
      
      &::after {
        content: '';
        position: absolute;
        width: 200%;
        height: 200%;
        left: -50%;
        top: -50%;
        background: rgba(0, 122, 255, 0.3);
        border-radius: 50%;
        animation: pulse 2s infinite;
      }
    }

    .marker-label {
      position: absolute;
      left: 30rpx;
      top: 50%;
      transform: translateY(-50%);
      white-space: nowrap;
      font-size: 24rpx;
      color: #333;
      background: rgba(255, 255, 255, 0.9);
      padding: 4rpx 12rpx;
      border-radius: 10rpx;
      box-shadow: 0 2rpx 6rpx rgba(0, 0, 0, 0.1);
    }
  }

  .department-popup {
    position: fixed;
    bottom: 0;
    left: 0;
    right: 0;
    background: white;
    border-radius: 30rpx 30rpx 0 0;
    padding: 30rpx;
    z-index: 1000;
    box-shadow: 0 -4rpx 20rpx rgba(0, 0, 0, 0.1);

    .popup-header {
      display: flex;
      justify-content: space-between;
      align-items: center;
      margin-bottom: 20rpx;

      .popup-title {
        font-size: 32rpx;
        font-weight: bold;
        color: #333;
      }

      .popup-close {
        font-size: 40rpx;
        color: #999;
        padding: 10rpx;
      }
    }

    .popup-content {
      .info-item {
        margin-bottom: 16rpx;
        font-size: 28rpx;
        color: #666;

        .label {
          color: #333;
          font-weight: 500;
        }
      }

      .department-description {
        margin-top: 20rpx;
        font-size: 26rpx;
        color: #666;
        line-height: 1.6;
      }
    }
  }

  .controls-hint {
    position: fixed;
    bottom: 40rpx;
    left: 50%;
    transform: translateX(-50%);
    background: rgba(0, 0, 0, 0.6);
    color: white;
    padding: 10rpx 30rpx;
    border-radius: 30rpx;
    font-size: 24rpx;
    white-space: nowrap;
  }
}

@keyframes pulse {
  0% {
    transform: scale(1);
    opacity: 1;
  }
  100% {
    transform: scale(1.5);
    opacity: 0;
  }
}
</style> 